package contactbook.service.impl;

import contactbook.dao.AccountDAOInterface;
import contactbook.dao.ContactDAOInterface;
import contactbook.dao.impl.AccountDAO;
import contactbook.dao.impl.ContactDAO;
import contactbook.model.Account;
import contactbook.model.Contact;
import contactbook.service.ContactServiceInterface;

public class ContactService implements ContactServiceInterface {

	private ContactDAOInterface contactDAO;
	private AccountDAOInterface accountDAO;
	private Account account;
	
	public ContactService(Account account) {
		contactDAO = new ContactDAO();
		accountDAO = new AccountDAO();
		this.account = account;
	}
	
	@Override
	public Contact affectGroup(int contactId, int groupId) {
		if (account.isLogged()) {
			contactDAO.affectGroup(contactId, groupId);
			return contactDAO.getContactById(contactId);
		}
		else return null;
	}
	
	@Override
	public Contact addContact(Contact contact, int groupId) {
		if (account.isLogged()) {
			Contact c = contactDAO.addContact(contact, groupId);
			accountDAO.affectContact(account.getId(), c.getId());
			return c;
		}
		else return null;
	}

	@Override
	public void delete(int contactId) {
		if (account.isLogged())
		{
			contactDAO.delete(contactId);
			accountDAO.deleteContact(account.getId(), contactId);
		}
	}

	@Override
	public Contact getContactById(int contactId) {
		return (account.isLogged()) ? contactDAO.getContactById(contactId) : null;
	}

	@Override
	public Contact modify(int contactId, Contact temp) {
		if (account.isLogged()) {
			contactDAO.modify(contactId, temp);
			return contactDAO.getContactById(contactId);
		}
		else return null;
	}
	
	public boolean exists(int contactId) {
		return (account.isLogged()) ? contactDAO.exists(contactId) : null;
	}
	
	public boolean exists(Contact c) {
		return (account.isLogged()) ? contactDAO.exists(c) : false;
	}
}
